/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity.services;

import db.DBContext;
import db.mysql.DataRow;
import db.mysql.DataTable;
import db.mysql.DeleteExecutor;
import db.mysql.SelectExecutor;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import org.joda.time.DateTime;
import pgentity.User;
import pgentity.UserTempData;
import share.PGHelper;
import share.PGMacro;
import share.TimeUtil;

/**
 *
 * @author Salm
 */
public class RemoveInactiveUser extends TimerTask {
    private static final String TABLE = "A1LOGINFORCHECKINACTIVE";
    private static final int MAX_INACTIVE_DAY = 60;
    
    private final DateTime toDay;
    private final DateTime checkDay;
    
    private RemoveInactiveUser()
    {
        this.toDay = DateTime.now();
        this.checkDay = toDay.minusDays(MAX_INACTIVE_DAY + 1);
    }

    @Override
    public void run() {
        String sCheckDate = TimeUtil.formatDate(checkDay.toDate());
        DataTable dt = (DataTable) DBContext.SQL().execute(SelectExecutor.fromDesc(
                TABLE,
                String.format("_date = DATE(\'%s\')", sCheckDate),
                "_uid"));
        
        for (DataRow row : dt) {
            final String uid = row.get("_uid");
            Runnable checkRmv = new CheckRemoveUser(uid, toDay.getMillis());
            checkRmv.run();
        }
        
        DBContext.SQL().execute(DeleteExecutor.fromDesc(
                TABLE,
                String.format("_date = DATE(\'%s\')", sCheckDate)));
    }
    
    private static class CheckRemoveUser implements Runnable
    {
        private static final long MAX_INACTIVE_TIME = MAX_INACTIVE_DAY * 24 * 60 * 60 * 1000L;
        private static final int MIN_INACTIVE_LEVEL = 5;
        private final String uid;
        private final long now;

        public CheckRemoveUser(String uid, long now) {
            this.uid = uid;
            this.now = now;
        }

        @Override
        public void run() {
            try
            {
                UserTempData data = UserTempData.getTempData(uid);
                long lastLogin = PGHelper.toLong(data.getData(PGMacro.LAST_LOGIN));
                if (lastLogin + MAX_INACTIVE_TIME > now)
                    return;

                User user = User.getUser(uid);
                if (user.getLevel() >= MIN_INACTIVE_LEVEL)
                    return;

                UserServices.inst().destroyUser(uid);
            }
            finally
            {
//                EntityPool.inst().releaseAllThreadResources();
            }
        }
    }
    
    public static void startSchedule()
    {
        Timer scheduler = new Timer();
        
        Calendar midnight = Calendar.getInstance();
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        
        scheduler.scheduleAtFixedRate(new RemoveInactiveUser(),
                midnight.getTime(),
                TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
    }
}
